package com.xianbeiying.app.ckgl.data;

import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.QueZQBean;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.util.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class DateStatusManager {
	private Map<String, List<DateStatus>> farm2DateStatus = new HashMap<String,List<DateStatus>>();
	
	private static DateStatusManager instance = null;
	
	private DateStatusManager()
	{
		
	}
	
	public static DateStatusManager getInstance()
	{
		if (instance == null)
		{
			instance = new DateStatusManager();
		}
		
		return instance;
	}
	public void putDateStatus(Date startTime, Date endTime, long duration, long period, String farmid)
	{
		long sTime = startTime.getTime();
		long eTime = endTime.getTime();
		List<DateStatus> dateStatusList = new ArrayList<DateStatus>();
		while (sTime < eTime)
		{
			DateStatus dateStatus = new DateStatus();
			dateStatus.setStartTime(sTime);
			dateStatus.setEndTime(sTime + duration * 60 * 1000);
			dateStatusList.add(dateStatus);
			sTime += (period * 60 * 1000);
		}
		LogUtils.e(farmid+"---",dateStatusList.size()+"====");
		
		farm2DateStatus.put(farmid, dateStatusList);
	}
	
	public List<String> getRuningStatusFarm()
	{
		List<String> result = new ArrayList<String>();
		Iterator<Entry<String, List<DateStatus>>> iterator = farm2DateStatus.entrySet().iterator();
		while(iterator.hasNext())
		{
			Entry<String, List<DateStatus>> entry = iterator.next();
			List<DateStatus> dateStatusList = entry.getValue();
			for (DateStatus date : dateStatusList)
			{
				if (date.isRunning())
				{
					result.add(entry.getKey());
					break;
				}
			}
		}
		
		return result;
	}
	
	public String getNextRunFarm()
	{
		String farm = "";
		Iterator<Entry<String, List<DateStatus>>> iterator = farm2DateStatus.entrySet().iterator();
		long instance = 0;
		while(iterator.hasNext())
		{
			Entry<String, List<DateStatus>> entry = iterator.next();
			List<DateStatus> dateStatusList = entry.getValue();
			for (DateStatus date : dateStatusList)
			{
				if (date.getDistanceByStartTime() > 0 )
				{
					if (instance == 0 || date.getDistanceByStartTime() < instance)
					{
						instance = date.getDistanceByStartTime();
						farm = entry.getKey();
					}
				}
			}
		}
		return farm;
	}
	
	public Set<String> getAllFarms()
	{
		return farm2DateStatus.keySet();
	}
	
	public void clearDateStatus()
	{
		this.farm2DateStatus.clear();
	}

	//已完成的判断 当前时间点进行比较 用最小值进行判断
     public ArrayList<String> getEndList(DevicesBean devicesBean,QueZQBean queZQBean){
		 ArrayList<String> getEnd=new ArrayList<>();
		 getEnd.clear();
		 int size=queZQBean.getV().getPLANS().size();
		 int sizess=devicesBean.getALLDEVICES().size();
		 for(int i=0;i<size;i++){
			 int endTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(queZQBean.getV().getPLANS().get(i).getENDTIME(),"yyyy-MM-dd HH:mm");
			 int downTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(DataUtil.dateHTime(),"yyyy-MM-dd HH:mm");
			 if(downTime>endTime){
				 for(int j=0;j<sizess;j++){
					 if(devicesBean.getALLDEVICES().get(j).getDevicetype()==1||devicesBean.getALLDEVICES().get(j).getDevicetype()==0){
					  if(devicesBean.getALLDEVICES().get(j).getSystemid()==queZQBean.getV().getPLANS().get(i).getSYSTEMID()&&
							  devicesBean.getALLDEVICES().get(j).getDeviceid()==queZQBean.getV().getPLANS().get(i).getDEVID()){
						  getEnd.add( devicesBean.getALLDEVICES().get(j).getName());
					  }}
				 }
			 }

		 }
		 return getEnd;
	 }
	//正在进行的判断  用最小值进行判断和最大值进行判断
	public ArrayList<String> getPlantList(DevicesBean devicesBean,QueZQBean queZQBean){
		ArrayList<String> getPlant=new ArrayList<>();
		getPlant.clear();
		int size=queZQBean.getV().getPLANS().size();
		int sizess=devicesBean.getALLDEVICES().size();
		for(int i=0;i<size;i++){
			int startTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(queZQBean.getV().getPLANS().get(i).getBEGINTIME(),"yyyy-MM-dd HH:mm");
			int downTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(DataUtil.dateHTime(),"yyyy-MM-dd HH:mm");
			int endTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(queZQBean.getV().getPLANS().get(i).getENDTIME(),"yyyy-MM-dd HH:mm");
			if(startTime<downTime&&downTime<endTime){
				for(int j=0;j<sizess;j++){
					if(devicesBean.getALLDEVICES().get(j).getDevicetype()==1||devicesBean.getALLDEVICES().get(j).getDevicetype()==0){
					if(devicesBean.getALLDEVICES().get(j).getSystemid()==queZQBean.getV().getPLANS().get(i).getSYSTEMID()&&
							devicesBean.getALLDEVICES().get(j).getDeviceid()==queZQBean.getV().getPLANS().get(i).getDEVID()){
						getPlant.add(devicesBean.getALLDEVICES().get(j).getName());
					}
					}
				}
			}
		}
		return getPlant;
	}
	//还未开始的进行判断 用最大值进行判断
	public ArrayList<String> getNextList(DevicesBean devicesBean,QueZQBean queZQBean){
		ArrayList<String> getNext=new ArrayList<>();
		getNext.clear();
		int size=queZQBean.getV().getPLANS().size();
		int sizess=devicesBean.getALLDEVICES().size();
		for(int i=0;i<size;i++){
			int startTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(queZQBean.getV().getPLANS().get(i).getBEGINTIME(),"yyyy-MM-dd HH:mm");
			int downTime=com.xianbeiying.app.ckgl.util.DataUtil.getTime(DataUtil.dateHTime(),"yyyy-MM-dd HH:mm");
			if(startTime>downTime){
				for(int j=0;j<sizess;j++){
					if(devicesBean.getALLDEVICES().get(j).getDevicetype()==1||devicesBean.getALLDEVICES().get(j).getDevicetype()==0){
						if(devicesBean.getALLDEVICES().get(j).getSystemid()==queZQBean.getV().getPLANS().get(i).getSYSTEMID()&&
								devicesBean.getALLDEVICES().get(j).getDeviceid()==queZQBean.getV().getPLANS().get(i).getDEVID()){
							getNext.add( devicesBean.getALLDEVICES().get(j).getName());
						}
					}

				}
			}

		}
		return getNext;
	}
}
